<?php 
error_reporting(0);

header("Content-Type: text/xml");
echo '<?xml version="1.0"?' .'>';

//Controller
//------------------------------------------------------------------

require_once("../../config.php");
require_once("../../inc/dblib/dblib.php");

$success = false;
$err =  '';

//Get Data
connectSQL();
try {
if($_POST['user'] == 'l34ndr0m1lm4nd4@@'){
	//
	runSQL('START TRANSACTION');
	
	try {
		//
		$sql = 	"INSERT INTO reclamos " . 
		   	"        (status, nombre, apellido, doc_tipo, doc, telefono, tipo, calle, calle_alt, gps_lat, gps_long)   " . 
		   	"VALUES  (%s,     '%s',  '%s',      %s,       %s,  '%s',     %s,   '%s',  '%s',      '%s',    '%s') ";
		$p = array();
		$p['status']= 0; // Default Status  $_POST['status'];
		$p['nombre']=$_POST['nombre'];
		$p['apellido']=$_POST['apellido'];
		$p['doc_tipo']=$_POST['doc_tipo'];
		$p['doc']=$_POST['doc'];
		$p['telefono']=$_POST['telefono'];
		$p['tipo']=$_POST['tipo'];
		$p['calle']=$_POST['calle'];
		$p['calle_alt']=$_POST['calle_alt'];
		$p['gps_lat']=$_POST['gps_lat'];
		$p['gps_long']=$_POST['gps_long'];
		runSQL($sql,$p);
		  
		$nroreclamo = 0; 
		
		$rs = runSQL('SELECT LAST_INSERT_ID()');
	
		while ($row = mysql_fetch_array($rs)) {
			$nroreclamo = $row[0];  
		}
	
		$success = ($nroreclamo > 0);
	
		runSQL('COMMIT');
	} catch (Exception $ex) {
		runSQL('ROLLBACK');
		$err = $ex->getMessage();
	}
} 
} catch(Exception $ee){
	$success = false;
	$err = $ex->getMessage();
}
disconnectSQL(); 

//Get Image
if($success){
	try {
		if ($_FILES['picture']['error'] == UPLOAD_ERR_OK){
			$ext = end(explode('.', strtolower($_FILES['picture']['name'])));
			
			if(!in_array($ext, $upload_allowed_ext)){
			   throw new Exception('Extension no admitida');
			}
			
			$fileName = 'r_' . $nroreclamo . '.' . $ext;
				
			/* Save the uploaded file if its size is greater than 0. */
			if ($_FILES['picture']['size'] > 0 && $_FILES['picture']['size'] < $upload_max_size){
			  	
			  	$fileName = basename($fileName);
			  	 
			  	$dirName = BASEFOLDER . $upload_path;
			 
			  	if (move_uploaded_file($_FILES['picture']['tmp_name'], $dirName . $fileName)){
			  	    $success = true;
			  	} else {
			  		$err = 'error interno al copiar el archivo';
			  	}
			} else {
				$err = 'longitud de archivo incorrecta';
		    }
		} else {
			$err = 'error al subiendo el archivo';
		}
	} catch(Exception $ex){
		$success = false;
		$err = $ex->getMessage();
	}
}


//View
//------------------------------------------------------------------
?><service><?php
if(!$success){
///
?>
<status>FAIL</status>
<error><?php echo $err; ?></error><?php
} else {
?>
<ticket><?php echo $nroreclamo; ?></ticket>
<file>SUCCESS</file><?php 
}
?>
</service>

